<?php

/** Zend_Controller_Action */
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';

class StatusController extends Soooner_Controller_Action
{
	private $_roleid;
	private $_modelStatus;

    public function init()
    {
        /* Initialize action controller here */
        $this->initDzTemplate();
    }

    public function delajaxAction()
    {
        $status = intval($this->_getParam('status'));
        if (empty($status))
        {
            exit;
        }
        $this->_modelStatus = new Model_Tables_Status();
        $r = $this->_modelStatus->mydelete($status);
        if (false==$r)
        {
            echo 'fail';exit;
        }
        else
        {
            echo 'd'.$status;exit;
        }
    }

    public function saveajaxAction()
    {
        $type = $this->_getParam('type');
        if (empty($type))
        {
            exit;
        }
        if ('name'==$type)
        {
            $request = $this->getRequest();
            $params = $request->getPost();
            $params['id'] = trim($params['id']);
            if (empty($params['id']))
            {
                exit;
            }
            $params['id'] = substr($params['id'], 1);
            $id = intval($params['id']);
            $name = stripslashes($params['value']);
            $this->_modelStatus = new Model_Tables_Status();
            $name1 = $this->_modelStatus->myupdateName($id, $name);
            echo $name;exit;
        }
        else if ('description'==$type)
        {
            $request = $this->getRequest();
            $params = $request->getPost();
            $params['id'] = trim($params['id']);
            if (empty($params['id']))
            {
                exit;
            }
            $params['id'] = substr($params['id'], 1);
            $id = intval($params['id']);
            $description = stripslashes($params['value']);
            $this->_modelStatus = new Model_Tables_Status();
            $this->_modelStatus->myupdateDescription($id, $description);
            echo $description;exit;
        }
//        else if ('delete'==$type)
//        {
//            $status = intval($this->_getParam('status'));
//            if (empty($status))
//            {
//                exit;
//            }
//            $this->_modelStatus = new Model_Tables_Status();
//            $r = $this->_modelStatus->mydelete($status);
//            if (false==$r)
//            {
//            	echo 'fail';exit;
//            }
//            else
//            {
//            	echo 'd'.$status;exit;
//            }
//        }
    }

    public function indexAction()
    {
        // action body
        $this->_modelStatus = new Model_Tables_Status();
        $result = $this->_modelStatus->mybrowse();
        include $this->_dzTemplate->getFile( 'status.index.html' );
    }

    public function addAction()
    {
        // action body
        $request = $this->getRequest();
        if ($request->isPost())
        {
            $params = $request->getPost();
            if (!Zend_Validate::is($params['info']['name'], 'StringLength', array(2, 40)))
            {
                throw new Exception('状态名称只能为 2-40 个字符');
            }
            if (!Zend_Validate::is($params['info']['description'], 'StringLength', array(0, 200)))
            {
                throw new Exception('状态描述最多为 200 个字符');
            }
            $params['info']['name'] = stripslashes($params['info']['name']);
            $params['info']['description'] = stripslashes($params['info']['description']);

            $this->_modelStatus = new Model_Tables_Status();
            $id = $this->_modelStatus->myadd($params['info']);
            $forward = empty($params['forward']) ? $params['forward'] : '/status/index/';
            $this->_redirect($forward);
        }

        $this->_modelStatus = new Model_Tables_Status();
        $listused = $this->_modelStatus->mygetsUsed();
        $listunused = array();
        for ($i=$this->_modelStatus->getStatusMin();$i<=$this->_modelStatus->getStatusMax();$i++)
        {
            if (!in_array($i, $listused))
            {
                $listunused[] = $i;
            }
        }
        include $this->_dzTemplate->getFile( 'status.add.html' );
    }

    public function editAction()
    {
        // action body
        $request = $this->getRequest();
        if ($request->isPost())
        {
            $params = $request->getPost();
            $status = intval($this->_getParam('status'));
            if (0>=$status)
            {
            	throw new Exception('状态值非法');
            }
            if (!Zend_Validate::is($params['info']['name'], 'StringLength', array(2, 40)))
            {
                throw new Exception('状态名称只能为 2-40 个字符');
            }
            if (!Zend_Validate::is($params['info']['description'], 'StringLength', array(0, 200)))
            {
                throw new Exception('状态描述最多为 200 个字符');
            }
            $params['info']['name'] = stripslashes($params['info']['name']);
            $params['info']['description'] = stripslashes($params['info']['description']);

            $this->_modelStatus = new Model_Tables_Status();
            $this->_modelStatus->myupdate($status, $params['info']);
            $forward = empty($params['forward']) ? $params['forward'] : '/status/index/';
            $this->_redirect($forward);
        }

        $status = intval($this->_getParam('status'));
        $this->_modelStatus = new Model_Tables_Status();
        $info = $this->_modelStatus->myget($status, '*');
        if (empty($info))
        {
        	throw new Exception('没有该记录');
        }
        if (isset($info['issystem']) && 0==$info['issystem'])
        {
        	//编辑自定义的状态，读取未使用的状态值列表
	        $listused = $this->_modelStatus->mygetsUsed();
	        $listunused = array();
	        for ($i=$this->_modelStatus->getStatusMin();$i<=$this->_modelStatus->getStatusMax();$i++)
	        {
	            if (!in_array($i, $listused))
	            {
	                $listunused[] = $i;
	            }
	        }
	        //将该状态值也加入列表
	        $listunused[] = $status;
	        sort($listunused);
        }
        include $this->_dzTemplate->getFile( 'status.edit.html' );
    }

}
